package com.shm.service.impl;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

import com.ruoyi.common.core.domain.entity.Campus;
import com.ruoyi.common.core.domain.entity.ProvinceCityRegion;
import com.ruoyi.common.core.domain.vo.CampusVo;
import com.ruoyi.common.utils.DateUtils;
import com.shm.service.IProvinceCityRegionService;
import org.apache.logging.log4j.ThreadContext;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.shm.mapper.CampusMapper;
import com.shm.service.ICampusService;

/**
 * 校区Service业务层处理
 *
 * @author dam
 * @date 2023-07-12
 */
@Service
public class CampusServiceImpl implements ICampusService {
    @Autowired
    private CampusMapper campusMapper;
    @Autowired
    private IProvinceCityRegionService provinceCityRegionService;

    /**
     * 查询校区
     *
     * @param id 校区主键
     * @return 校区
     */
    @Override
    public Campus selectCampusById(Long id) {
        return campusMapper.selectCampusById(id);
    }

    /**
     * 查询校区列表
     *
     * @param campus 校区
     * @return 校区
     */
    @Override
    public List<Campus> selectCampusList(Campus campus) {
        return campusMapper.selectCampusList(campus);
    }

    @Override
    public List<CampusVo> selectCampusVoList(CampusVo campusVo) {
        List<CampusVo> campusVos = campusMapper.selectCampusVoList(campusVo);
        Set<Long> idSet = new HashSet<>();
        for (CampusVo vo : campusVos) {
            idSet.add(vo.getProvinceId());
            idSet.add(vo.getCityId());
            idSet.add(vo.getRegionId());
        }
        if (idSet.size() != 0) {
            List<ProvinceCityRegion> provinceCityRegions = provinceCityRegionService.selectProvinceCityRegionByIds(idSet.toArray(new Long[]{}));
            Map<Long, String> idAndNameMap = new HashMap<>();
            for (ProvinceCityRegion provinceCityRegion : provinceCityRegions) {
                idAndNameMap.put(provinceCityRegion.getId(), provinceCityRegion.getName());
            }
            for (CampusVo vo : campusVos) {
                vo.setProvinceName(idAndNameMap.get(vo.getProvinceId()));
                vo.setRegionName(idAndNameMap.get(vo.getRegionId()));
                vo.setCityName(idAndNameMap.get(vo.getCityId()));
            }
        }

        return campusVos;
    }

    /**
     * 新增校区
     *
     * @param campus 校区
     * @return 结果
     */
    @Override
    public int insertCampus(Campus campus) {
        campus.setCreateTime(DateUtils.getNowDate());
        return campusMapper.insertCampus(campus);
    }

    /**
     * 修改校区
     *
     * @param campus 校区
     * @return 结果
     */
    @Override
    public int updateCampus(Campus campus) {
        campus.setUpdateTime(DateUtils.getNowDate());
        return campusMapper.updateCampus(campus);
    }

    /**
     * 批量删除校区
     *
     * @param ids 需要删除的校区主键
     * @return 结果
     */
    @Override
    public int deleteCampusByIds(Long[] ids) {
        return campusMapper.deleteCampusByIds(ids);
    }

    /**
     * 删除校区信息
     *
     * @param id 校区主键
     * @return 结果
     */
    @Override
    public int deleteCampusById(Long id) {
        return campusMapper.deleteCampusById(id);
    }


}
